package com.changfeng.controller;

import com.changfeng.bean.dto.vo.BrandListVO;
import com.changfeng.bean.dto.vo.BrandVO;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public List<BrandListVO> findAll() {
        return brandService.findListAll();
    }

    @PostMapping("/saveBrand")
    public void saveBrand(@RequestBody BrandListVO vo) {
        if (vo == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        if (vo.getBrandId() != null) {
            brandService.updateBrand(vo);
        } else {
            brandService.saveBrand(vo);
        }

    }



    @GetMapping("/deleteBrand")
    public void deleteBrand(Integer brandId) {
        if (brandId == null) {
            throw  new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        brandService.deleteBrand(brandId);
    }


    @GetMapping("/getBrandById")
    public BrandVO getBrandById(Integer brandId) {

        if (brandId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        return brandService.findBrandById(brandId);
    }

    @GetMapping("/addCategory")
    public void addCategory(Integer brandId, Integer categoryId) {

        if (brandId==null || categoryId ==null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        brandService.addCategory(brandId, categoryId);
    }


    @GetMapping("/deleteCategory")
    public void deleteCategory(Integer brandId, Integer categoryId) {
        if (brandId==null || categoryId ==null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        brandService.deleteCategory(brandId, categoryId);
    }
}
